package com.tangcl.tclmvvm.activity

import android.view.MenuItem
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.tangcl.module_commom.base.BaseActivity
import com.tangcl.tclmvvm.R
import com.tangcl.tclmvvm.adapter.MainFragmentAdapter
import com.tangcl.tclmvvm.fragment.HomeFragment
import com.tangcl.tclmvvm.fragment.MineFragment
import com.tangcl.tclmvvm.utils.Constants
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.bottom_nav_view
import kotlinx.android.synthetic.main.activity_main_viewpage2.*

class MainActivity : BaseActivity() {
    //0:BottomNavigationView + viewpager2  1:BottomNavigationView + Navigation
    private val layoutType = 0

    override fun getLayoutId(): Int {
        return when (layoutType) {
            1 -> {
                R.layout.activity_main
            }
            else -> {
                R.layout.activity_main_viewpage2
            }
        }

    }

    override fun initView() {
        when (layoutType) {
            1 -> {
                initNavigationView(1)
            }
            else -> {
                initBottomNavigationViewWithVewPager()
            }
        }
    }

    override fun setListener() {
        when (layoutType) {
            1 -> {
//                likeBtnClick(TextView(this))
            }
            else -> {

            }
        }

    }

    /**
     * 初始化 Navigation和 BottomNavigationView
     */
    private fun initNavigationView(type: Int) {
        when (type) {
            1 -> {
                //防止fragment未构建完成
                fragment.post(Runnable {
                    val navController = Navigation.findNavController(this, R.id.fragment)
                    NavigationUI.setupWithNavController(bottom_nav_view, navController)
                })
            }
            else -> {
                //fragment的容器视图，navHost的默认实现——NavHostFragment
                val navHostFragment =
                    supportFragmentManager.findFragmentById(R.id.fragment) as NavHostFragment
                //管理应用导航的对象
                val navController = navHostFragment.navController
                //fragment与BottomNavigationView的交互交给NavigationUI
                bottom_nav_view.setupWithNavController(navController)
            }
        }

    }

    /**
     * 初始化 viewpager2和 BottomNavigationView
     */
    private fun initBottomNavigationViewWithVewPager() {
        val fragmentList = mutableListOf<Fragment>()
        fragmentList.add(HomeFragment.newInstance("", ""))
        fragmentList.add(MineFragment.newInstance("", ""))
        val mAdapter = MainFragmentAdapter(supportFragmentManager, lifecycle, fragmentList)
        viewpager2.adapter = mAdapter
        viewpager2.isUserInputEnabled = true
        viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                bottom_nav_view_2.menu.getItem(position).isChecked = true
            }
        })

        bottom_nav_view_2.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.homeFragment -> {
                    viewpager2.setCurrentItem(0, true)
                    true
                }
                R.id.mineFragment -> {
                    viewpager2.setCurrentItem(1, true)
                    true
                }
                else -> {
                    false
                }
            }
        }

    }

    /**
     * 模拟按钮点击切换
     */
    private fun likeBtnClick(view: View) {
        view.setOnClickListener {
            val fragment =
                HomeFragment.newInstance("p1", "p2")
            fragment.mActivity.let {
            }
        }
    }

}